package com.etc.view;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import com.etc.controller.SerializeOperation;
import com.etc.entity.Counter;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.awt.event.ActionEvent;

public class Register {

	public void placeComponents(JPanel panel, DataOutputStream dos, DataInputStream dis, JFrame jFrame) {
		panel.setLayout(null);
		panel.setBackground(Color.lightGray);
		
		JLabel userLabel = new JLabel("账号：");
		userLabel.setBounds(50, 50, 80, 25);
		panel.add(userLabel);
		
		JTextField userText = new JTextField(20);
		userText.setBounds(100, 50, 165, 25);
		panel.add(userText);
		
		JLabel pwdLabel = new JLabel("密码：");
		pwdLabel.setBounds(50, 80, 80, 25);
		panel.add(pwdLabel);

		JPasswordField pwdText = new JPasswordField(20);
		pwdText.setBounds(100, 80, 165, 25);
		panel.add(pwdText);
		
		JLabel pwdLabel2 = new JLabel("重复密码：");
		pwdLabel2.setBounds(40, 110, 80, 25);
		panel.add(pwdLabel2);

		JPasswordField pwdText2 = new JPasswordField(20);
		pwdText2.setBounds(100, 110, 165, 25);
		panel.add(pwdText2);
		
		JButton registerButton = new JButton("注册");
		registerButton.setBounds(150, 150, 80, 25);
		panel.add(registerButton);
		
		registerButton.addActionListener(e->{
			if(!pwdText.getText().equals(pwdText2.getText())) {
				JOptionPane.showMessageDialog(panel, "两次密码输入不一致", "注册失败", JOptionPane.ERROR_MESSAGE);
			}else {
				String str = "register#" + userText.getText() + "#" + pwdText.getText();
				String restr = sendData(str, panel, dos, dis);
				if(restr.equals("error")|| restr==null) {
					JOptionPane.showMessageDialog(panel, "用户已经存在请重新输入", "注册失败", JOptionPane.ERROR_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(panel, "恭喜你注册成功", "注册成功", JOptionPane.INFORMATION_MESSAGE);
					Counter counter = SerializeOperation.loadFromFile(restr);
					GameLoading gl=new GameLoading(counter, dos, dis, panel);
					gl.setTitle(counter.getLoginId());
					gl.pack();
					gl.setVisible(true);
					gl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					gl.setLocationRelativeTo(null);
					jFrame.setVisible(false);
				}
			}
		});
	}

	public String sendData(String str, JPanel panel,  DataOutputStream dos, DataInputStream dis) {
        try {
            dos.writeUTF(str);
            dos.flush();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(panel, "发送数据失败，可能连接已断开", "ERROR", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
            System.exit(0);
        }

        String retStr = null;
        try {
            retStr = dis.readUTF();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(panel, "接收数据失败，可能连接已断开", "ERROR", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
            System.exit(0);
        }
        return retStr;
    }
	
	public static void main(String[] args) {
		
//		JFrame frame = new JFrame("注册界面");
//		frame.setBounds(100, 100, 400, 300);
//		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		Register reg = new Register();
//		JPanel panel=new JPanel();
//		frame.add(panel);
//	
//		reg.placeComponents(panel);
//
//		frame.setVisible(true);
	}
}
